###############################################################################
# SuperLinks                                                                  #
# Copyright (c) 2000-2007 Greg Nolle (http://greg.nolle.co.uk)                #
###############################################################################
# This program is free software; you can redistribute it and/or modify it     #
# under the terms of the GNU General Public License as published by the Free  #
# Software Foundation; either version 2 of the License, or (at your option)   #
# any later version.                                                          #
#                                                                             #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   #
# more details.                                                               ##                                                                             #
# You should have received a copy of the GNU General Public License along     #
# with this program; if not, write to the Free Software Foundation, Inc.,     #
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 #
###############################################################################
# ControlPanel/Output/Email.pm.pl -> Email module                             #
###############################################################################
# DON'T EDIT BELOW UNLESS YOU KNOW WHAT YOU'RE DOING!                         #
###############################################################################
package Skin::CP::Email;

BEGIN { require "System.pm.pl"; import System qw($SYSTEM); }
BEGIN { require "General.pm.pl"; import General qw($GENERAL); }
require "Standard.pm.pl";

use HTML::Dialog;
use strict;

sub new {
  my ($class) = @_;
  my $self    = {};
  return bless ($self, $class);
}

sub DESTROY { }

###############################################################################
# show subroutine
sub show {
  my $self = shift;
  my %in = (input => undef, @_);

  my $LANGUAGE = {
    "MISSING" => {
      "SUBJECT"       => qq~<li>You didn't fill in the "Subject" field.~,
      "MESSAGE"       => qq~<li>You didn't fill in the "Message" field.~,
      "EMAILS"        => qq~<li>You didn't select any recipients.~
    },
    "ERROR"         => qq~<p><font class="error-body">There were errors:<ul>[%error%]</ul></font>~
  };

  $in{'error'} = &Standard::ProcessError(LANGUAGE => $LANGUAGE, ERROR => $in{'error'});

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body = $Dialog->TableRow(items => [{ body => "Please fill out all the required (<font class=\"form-required\"><b>*</b></font>) fields:".$in{'error'}, colspan => "2" }]);

  my (@options, @values);
  foreach my $key (keys %{ $in{'input'}->{'CONTACTS'} }) {
    my $contact = $in{'input'}->{'CONTACTS'}->{$key};
    push(@options, { value => $key, label => $contact->[0]->{'CONTACT_NAME'}." (".$key.")" });
    push(@values, $key);
  }

  $Body .= $Dialog->FormSelectBox(
    name      => "FORM_EMAILS",
    value     => \@values,
    subject   => "Recipients",
    required  => 1,
    multiple  => 1,
    rows      => 8,
    items     => \@options
  );

  $Body .= $Dialog->FormTextBox(
    name      => "FORM_SUBJECT",
    subject   => "Subject",
    required  => 1
  );

  $Body .= $Dialog->FormTextArea(
    name      => "FORM_MESSAGE",
    value     => "Dear [%name%],\n\n\n---\nYou are receiving this email because one or more of your resources (ID number(s): [%ids.join(\", \")%]) is listed at [%general.LINKS_TITLE%] and you indicated that you wanted to receive emails from us. To stop receiving these emails, please visit [%system.SCRIPT_URL%]?action=Modify",
    subject   => "Message",
    required  => 1,
    rows      => 15
  );

  $Body .= $Dialog->FormCheckBox(
    name      => "FORM_HTML",
    items     => [
      { value => "1", label => "Message uses HTML?" }
    ]
  );

  $Body .= $Dialog->FormButton(
    items => [
      { type => "submit", value => "Send" },
      { type => "reset", value => "Cancel" }
    ]
  );

  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body    => $Body,
    header  => "Email List Members",
    form    => 1,
    hiddens => [
      { name => "action", value => "DoEmail" },
      { name => "CP", value => "1" },
      { name => "SID", value => $SL::ADMIN{'SID'} }
    ]
  );
  
  return $Body;
}

###############################################################################
# do subroutine
sub do {
  my $self = shift;
  my ($section) = @_;

  #----------------------------------------------------------------------#
  # Printing page...                                                     #

  my $Dialog = HTML::Dialog->new();

  my $Body  = $Dialog->TableRow(items => [{ body => "Emailing list members:", colspan => 2 }]);
     $Body .= $Dialog->TableRow(items => [{ body => "<iframe src=\"#\" name=\"LOG\" height=\"300\" width=\"100%\"></iframe>", colspan => 2 }]);
  
  foreach my $key (keys %SL::QUERY) {
    $Body .= $SL::CGI->hidden(-name => $key);
  }

  $Body .= $Dialog->FormButton(
    items => [
      { type => "button", value => "Close", extra => { onClick => "window.location='".($SL::QUERY{'Referer'} || "$SYSTEM->{'SCRIPT_URL'}?CP=1&action=Email&SID=$SL::ADMIN{'SID'}")."';" } }
    ]
  );
  
  $Body = $Dialog->Table(body => $Body);
  $Body = $Dialog->StandardPage(
    body        => $Body,
    header      => "Email List Members",
    body_extra  => "onLoad=\"form.submit();\"",
    form        => 1,
    form_extra  => { target => "LOG" },
    hiddens     => [
      { name => "do", value => "1" }
    ]
  );
  
  return $Body;
}

###############################################################################
# email subroutine
sub email {
  my $self = shift;
  my %in = (type => "", input => undef, @_);

  #----------------------------------------------------------------------#
  # Preparing data...                                                    #

  my %FIELDS;

  $FIELDS{'name'}      = $in{'input'}->{'RESOURCES'}->[0]->{'CONTACT_NAME'};
  $FIELDS{'email'}     = $in{'input'}->{'RESOURCES'}->[0]->{'CONTACT_EMAIL'};
  $FIELDS{'resources'} = $in{'input'}->{'RESOURCES'};
  
  foreach my $resource (@{ $in{'input'}->{'RESOURCES'} }) {
    push(@{ $FIELDS{'ids'} }, $resource->{'ID'});
  }

  #----------------------------------------------------------------------#
  # Printing email...                                                    #

  my %Return;

  $Return{'MESSAGE'} = &Standard::Substitute(
    INPUT     => \$in{'input'}->{'RECORD'}->{'MESSAGE'},
    STANDARD  => 1,
    FIELDS    => \%FIELDS
  );
  $Return{'TO'} = $in{'input'}->{'RESOURCES'}->[0]->{'CONTACT_EMAIL'};
  $Return{'SUBJECT'} = &Standard::Substitute(
    INPUT     => \$in{'input'}->{'RECORD'}->{'SUBJECT'},
    STANDARD  => 1,
    FIELDS    => \%FIELDS
  );
  $Return{'HTML'} = $in{'input'}->{'RECORD'}->{'HTML'};

  return %Return;
}

1;